/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Boleta_Excepciones.java
 *
 * Created on 07-nov-2011, 13:05:17
 */
package presentacion;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Manuelita
 */
public class Boleta_Excepciones extends javax.swing.JFrame {

    private Control control;
    Date fechaActual = new Date();//esta clase es para manejar la fecha del sistema
    SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy"); //esta nos permite definir el formato de la fecha
    String cadenaFecha = formato.format(fechaActual);//Aqui asignamos la fecha del sistema en el String cadenaFecha
    /** Creates new form Boleta_Excepciones */
    public Boleta_Excepciones() {
        initComponents();
        setLocationRelativeTo(null);
        this.mensajeAyuda();
        this.txt_FechaInicio.setText(cadenaFecha);
        this.txt_Otro.setEditable(false);
    }
    
    private void mensajeAyuda(){
        this.cb_Motivo.setToolTipText("Debe elegir un motivo de excepcion");
        this.txt_Otro.setToolTipText("Especifique el Otro Motivo");
        this.txt_FechaInicio.setToolTipText("Escriba la fecha del día de hoy bajo el formato: \"XX/XX/XX\"");
        this.txt_FechaFinal.setToolTipText("Escriba la fecha del día de hoy bajo el formato: \"XX/XX/XX\"");
    }

    /**
     * 
     * @param computadoras
     */
    public void cargarTabla(ArrayList computadoras){
        for(int i = 0; i < computadoras.size(); i++){
            this.tb_TablaExcepciones.setValueAt(computadoras.get(i), i, 0);
            this.tb_TablaExcepciones.setValueAt("Desactivada", i, 1);
        }
    }
    
    /**
     * 
     * @param tabla
     */
    public void getControl(Control tabla){
        this.control = tabla;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lb_Excepion = new javax.swing.JLabel();
        cb_Motivo = new javax.swing.JComboBox();
        lb_Otro = new javax.swing.JLabel();
        txt_Otro = new javax.swing.JTextField();
        lb_FechaInicio = new javax.swing.JLabel();
        txt_FechaInicio = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tb_TablaExcepciones = new javax.swing.JTable();
        lb_FechaFinal = new javax.swing.JLabel();
        txt_FechaFinal = new javax.swing.JTextField();
        lb_Periodo = new javax.swing.JLabel();
        bt_Aceptar = new javax.swing.JButton();
        lb_Motivo = new javax.swing.JLabel();
        txt_Motivo = new javax.swing.JTextField();
        bt_Cancelar = new javax.swing.JButton();

        setTitle("Control de Excepciones");

        lb_Excepion.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lb_Excepion.setText("Excepción:");

        cb_Motivo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Prestamo", "Reparacion", "Otro" }));
        cb_Motivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_MotivoActionPerformed(evt);
            }
        });

        lb_Otro.setFont(new java.awt.Font("Tahoma", 1, 11));
        lb_Otro.setText("Otro, Especifique:");

        lb_FechaInicio.setFont(new java.awt.Font("Tahoma", 1, 11));
        lb_FechaInicio.setText("Fecha Inicio:");

        txt_FechaInicio.setEditable(false);

        tb_TablaExcepciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Computadora", "Estado", "Seleccionada", "Fecha Inicio", "Fecha Final"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tb_TablaExcepciones);

        lb_FechaFinal.setFont(new java.awt.Font("Tahoma", 1, 11));
        lb_FechaFinal.setText("Fecha Final:");

        lb_Periodo.setFont(new java.awt.Font("Tahoma", 1, 11));
        lb_Periodo.setText("Periodo Fuera de Servicio:");

        bt_Aceptar.setFont(new java.awt.Font("Tahoma", 1, 11));
        bt_Aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentacion/imagenes/Aceptar.png"))); // NOI18N
        bt_Aceptar.setText("Aceptar");
        bt_Aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_AceptarActionPerformed(evt);
            }
        });

        lb_Motivo.setFont(new java.awt.Font("Tahoma", 1, 11));
        lb_Motivo.setText("Motivo:");

        bt_Cancelar.setFont(new java.awt.Font("Tahoma", 1, 11));
        bt_Cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentacion/imagenes/Cancelar.png"))); // NOI18N
        bt_Cancelar.setText("Cancelar");
        bt_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_CancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
                    .addComponent(lb_Excepion)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cb_Motivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lb_Otro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_Otro, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
                    .addComponent(lb_Periodo)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lb_Motivo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_Motivo, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lb_FechaInicio)
                        .addGap(18, 18, 18)
                        .addComponent(txt_FechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lb_FechaFinal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txt_FechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bt_Cancelar)
                        .addGap(18, 18, 18)
                        .addComponent(bt_Aceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lb_Excepion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cb_Motivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lb_Otro)
                    .addComponent(txt_Otro, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lb_Motivo)
                    .addComponent(txt_Motivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lb_Periodo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_FechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lb_FechaInicio)
                    .addComponent(txt_FechaFinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lb_FechaFinal))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_Aceptar)
                    .addComponent(bt_Cancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean validarblancos(String motivo, String otro, String fInicio, String fFinal){
        boolean salida = false;
        if(motivo.equals("Otro")){
            if(!otro.equals("") && !fInicio.equals("") && !fFinal.equals(""))
                salida = true;
        }else if(!motivo.equals("Elije una opcion")){
            if(otro.equals("") && !fInicio.equals("") && !fFinal.equals(""))
                salida = true;
        }else
            salida = false;
        return salida;
    }
    
    private boolean validarCampos(String otro, String fInicio, String fFinal){
        boolean salida = false;
        if(this.validarOtro(otro) == true)
            salida = true;
        else if(this.validarFInicio(fInicio) == true)
            salida = true;
        else if(this.validarFFinal(fFinal) == true)
            salida = true;
        else
            salida = false;
        return salida;
    }
    
    private boolean validarOtro(String otro){
        Pattern p = Pattern.compile("^[A-Za-z]+$");
        Matcher m = p.matcher(otro);
        if(m.find())
            return true;
        else
            return false;
    }
    
    private boolean validarFInicio(String otro){
        Pattern p = Pattern.compile("\\d{2}\\/\\d{2}\\/\\d{2}");
        Matcher m = p.matcher(otro);
        if(m.find())
            return true;
        else
            return false;
    }
    
    private boolean validarFFinal(String otro){
        Pattern p = Pattern.compile("\\d{2}\\/\\d{2}\\/\\d{2}");
        Matcher m = p.matcher(otro);
        if(m.find())
            return true;
        else
            return false;
    }
    
    private void bt_AceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_AceptarActionPerformed
        if(this.validarblancos(cb_Motivo.getSelectedItem().toString(), txt_Otro.getText(),
                txt_FechaInicio.getText(), txt_FechaFinal.getText()) == false){
            JOptionPane.showMessageDialog(rootPane, "Debe llenar los campos primero");
            return;
        }
        if(this.validarCampos(txt_Otro.getText(),
                txt_FechaInicio.getText(), txt_FechaFinal.getText()) == false){
            JOptionPane.showMessageDialog(rootPane, "Algunos campos tienen un formato incorrecto");
            return;
        }
        ArrayList seleccionadas = new ArrayList();
        String motivo = this.cb_Motivo.getSelectedItem().toString();
        for(int i = 0; i < tb_TablaExcepciones.getRowCount(); i++){
            Object valor = tb_TablaExcepciones.getValueAt(i, 2);
            if(valor != null)
                seleccionadas.add(tb_TablaExcepciones.getValueAt(i, 0));
        }
        this.control.actualizarDatos(seleccionadas, motivo, txt_FechaInicio.getText(), txt_FechaFinal.getText());
        this.control.cargarTabla();
        this.dispose();
        
        this.txt_FechaFinal.setText(cadenaFecha);
    }//GEN-LAST:event_bt_AceptarActionPerformed

    private void cb_MotivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_MotivoActionPerformed
        if(this.cb_Motivo.getSelectedItem().equals("Prestamo")){
            this.txt_Otro.setEditable(false);
        }
        if(this.cb_Motivo.getSelectedItem().equals("Reparacion")){
            this.txt_Otro.setEditable(false);
        }
        if(this.cb_Motivo.getSelectedItem().equals("Otro")){
            this.txt_Otro.setEditable(true);
        }
    }//GEN-LAST:event_cb_MotivoActionPerformed

private void bt_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_CancelarActionPerformed
    setVisible(false);
}//GEN-LAST:event_bt_CancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_Aceptar;
    private javax.swing.JButton bt_Cancelar;
    private javax.swing.JComboBox cb_Motivo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lb_Excepion;
    private javax.swing.JLabel lb_FechaFinal;
    private javax.swing.JLabel lb_FechaInicio;
    private javax.swing.JLabel lb_Motivo;
    private javax.swing.JLabel lb_Otro;
    private javax.swing.JLabel lb_Periodo;
    private javax.swing.JTable tb_TablaExcepciones;
    private javax.swing.JTextField txt_FechaFinal;
    private javax.swing.JTextField txt_FechaInicio;
    private javax.swing.JTextField txt_Motivo;
    private javax.swing.JTextField txt_Otro;
    // End of variables declaration//GEN-END:variables
}
